﻿$import( "SmitearWebGL.Programs.AbstractProgram" );

SmitearWebGL.Programs.LightPrePass.HBlurProgram = SmitearWebGL.Programs.AbstractProgram.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Programs.LightPrePass.HBlurProgram" );
	}
	,
	
	/******** public method ********/
	
	activeUniform : function( renderableObject )
	{
		this.context.gl.uniformMatrix3fv( this._shader.uniforms.uHBloomOffsets.location, false, renderableObject._hBloomOffsets );
		this.context.gl.uniformMatrix3fv( this._shader.uniforms.uHBloomWeights.location, false, renderableObject._hBloomWeights );
		
		var texture = renderableObject.getTextureAt( 0 );
		this.context.gl.activeTexture( this.context.gl.TEXTURE0 );
		this.context.gl.bindTexture( this.context.gl.TEXTURE_2D, texture.getGlTexture() );
		this.context.gl.uniform1i( this._shader.uniforms.uSampler.location, 0 );
		
		return SmitearWebGL.Core.HResult.SUCCESS;
	}
	,
	
	/******** private method ********/
	
	_generateVertexShaderStr : function()
	{
		this._super();
		
		var vertStr =
		"\
		attribute vec3 " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +";\n\
		varying vec2 vTexCoord;\n\
		void main(void)\n\
		{\n\
			vec4 pos = vec4(" + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +", 1.0);\n\
			gl_Position = pos;\n\
			vTexCoord = vec2((pos.x + 1.0) * 0.5, (pos.y + 1.0) * 0.5);\n\
		}\n\
		";
		
		return vertStr;
	}
	,
	_generateFragmentShaderStr : function()
	{
		this._super();
		
		var fragStr = 
		"\
		#ifdef GL_ES\n\
		precision highp float;\n\
		#endif\n\
		varying vec2 vTexCoord;\n\
		uniform mat3 uHBloomOffsets;\n\
		uniform mat3 uHBloomWeights;\n\
		uniform sampler2D uSampler;\n\
		void main(void) {\n\
			vec4 color = vec4(0.0, 0.0, 0.0, 0.0);\n\
			float hBloomOffsets[9];\n\
			hBloomOffsets[0] = uHBloomOffsets[0][0];\n\
			hBloomOffsets[1] = uHBloomOffsets[0][1];\n\
			hBloomOffsets[2] = uHBloomOffsets[0][2];\n\
			hBloomOffsets[3] = uHBloomOffsets[1][0];\n\
			hBloomOffsets[4] = uHBloomOffsets[1][1];\n\
			hBloomOffsets[5] = uHBloomOffsets[1][2];\n\
			hBloomOffsets[6] = uHBloomOffsets[2][0];\n\
			hBloomOffsets[7] = uHBloomOffsets[2][1];\n\
			hBloomOffsets[8] = uHBloomOffsets[2][2];\n\
			float hBloomWeights[9];\n\
			hBloomWeights[0] = uHBloomWeights[0][0];\n\
			hBloomWeights[1] = uHBloomWeights[0][1];\n\
			hBloomWeights[2] = uHBloomWeights[0][2];\n\
			hBloomWeights[3] = uHBloomWeights[1][0];\n\
			hBloomWeights[4] = uHBloomWeights[1][1];\n\
			hBloomWeights[5] = uHBloomWeights[1][2];\n\
			hBloomWeights[6] = uHBloomWeights[2][0];\n\
			hBloomWeights[7] = uHBloomWeights[2][1];\n\
			hBloomWeights[8] = uHBloomWeights[2][2];\n\
		    for( int i = 0; i < 9; i++ )\n\
		    {\n\
		        color += (texture2D( uSampler, vTexCoord + vec2( hBloomOffsets[i], 0.0 ) ) * hBloomWeights[i] );\n\
		    }\n\
		    gl_FragColor = vec4( color.rgb, 1.0 );\n\
		}\n\
		";
		
		return fragStr;
	}
}
);